package yl.hs.bmipfull.controllers.host;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.daos.vehiclemanage.ITruckInfoDao;
import yl.hs.bmipfull.pojo.production.ProductionLineEntity;
import yl.hs.bmipfull.pojo.production.ProductionTaskEntity;
import yl.hs.bmipfull.pojo.production.TransTaskEntity;
import yl.hs.bmipfull.security.HostValidation;
import yl.hs.bmipfull.service.laboratory.IProductOverageService;
import yl.hs.bmipfull.service.production.IProductShipOrderService;
import yl.hs.bmipfull.service.production.IProductionLineService;
import yl.hs.bmipfull.service.production.IProductionTaskService;
import yl.hs.bmipfull.service.production.ITransTaskService;
import yl.hs.bmipfull.service.vehiclemanage.ITruckInfoService;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author 谢东
 * @apiNote
 * @since 2022-05-12 10:16 创建
 */
@Api(value = "生产任务信息Web接口", tags = "主机程序——生产任务信息Web接口")
@RequestMapping("/host/productionTask")
@RestController
public class HostProductionTask extends ControllerBase {
    @Autowired
    IProductShipOrderService shipOrderService;

    @Autowired
    IProductionTaskService taskService;

    @Autowired
    ITransTaskService transTaskService;
    @Autowired
    ITruckInfoService truckInfoService;
    @Autowired
    IProductOverageService overageService;
    @Autowired
    IProductionLineService lineService;
    @ApiOperation(value = "查询生产进度", notes = "查询生产进度")
    @RequestMapping(value = "/getOfProgress", method = RequestMethod.GET)
    public WebResult getOfProgress() {
        try {
            ProductionLineEntity line = HostValidation.getLine();
            Map<String, Object> map = new HashMap<>();
            Date date = new Date();
            double todayVolume = shipOrderService.getDayVolume(date, line.getUseRang().split(",")[0]);
            double monthVolume = shipOrderService.getMonthVolume(date, line.getUseRang().split(",")[0]);
            //获取生产中的生产任务单
            List<ProductionTaskEntity> taskList = taskService.queryTaskSc(line.getUseRang().split(",")[0]);
            double planVolume = 0;
            List<String> snList = new ArrayList<>();
            if (taskList != null) {
                for (ProductionTaskEntity entity : taskList) {
                    planVolume = planVolume + Double.parseDouble(entity.getQuantity().toString());
                    snList.add(entity.getSn());
                }
                map.put("taskCount", taskList.size());
            } else {
                map.put("taskCount", 0);
            }
            //获取生产中的生产任务单的完成方量
            double completeVolume = shipOrderService.getVolumeByTaskSN(snList);
            double NotCompleteVolume = planVolume - completeVolume;
            //处理返回值
            map.put("todayVolume", todayVolume);
            map.put("monthVolume", monthVolume);
            map.put("planVolume", planVolume);
            map.put("completeVolume", completeVolume);
            map.put("NotCompleteVolume", NotCompleteVolume);
            return success("查询成功", map);
        } catch (Exception e) {
           return error(e);
        }
    }
    @ApiOperation(value = "获取待生产车辆信息", notes = "查询生产进度")
    @RequestMapping(value = "/getcar", method = RequestMethod.GET)
    public WebResult getCar(){
       var listtruck =  truckInfoService.selectByState((short) 10,"mixTruck");
       return success("查询成功",listtruck);
    }
    @ApiOperation(value = "查询退剩转信息", notes = "根据车辆编号查询退剩转信息")
    @RequestMapping(value = "/bytruckSN/{truckSN}", method = RequestMethod.GET)
    public WebResult findBytruckSN(@ApiParam(value = "车辆编号") @PathVariable String truckSN) {
        return success("查询成功", overageService.findByState(truckSN));
    }
    @ApiOperation(value = "保存运输任务,商砼可以拆为2条派车任务", notes = "查询/更新运输任务,商砼可以拆为2条派车任务信息")
    @RequestMapping(value = "/savestranstask", method = RequestMethod.PUT)
    public WebResult saveMenu(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            var task = new TransTaskEntity();
            var tasksn =  submitModel.getData().get("taskSN").toString();
            var truckSN =  submitModel.getData().get("truckSN").toString();
            var stfl =  submitModel.getData().get("stfl").toString();
            var sjfl =  submitModel.getData().get("sjfl").toString();
            var yl =  submitModel.getData().get("yl").toString();
            var ylstate =  submitModel.getData().get("ylstate").toString();
            var plname =  submitModel.getData().get("plname").toString();
            var taskentity = taskService.findBySn(tasksn);
            var truckentity = truckInfoService.findBySn(truckSN);
            var lineentity = lineService.findByName(plname);
            task.setAppendWeight(new BigDecimal(sjfl));
            if(yl.equals("0")){
                task.setOverageHandle((short) 20);
                task.setOverageSN("");
                task.setOverageWeight(BigDecimal.valueOf(0));
            }else{
                var ylsn =  submitModel.getData().get("ylsn").toString();
                if(Boolean.valueOf(ylstate)){
                    task.setOverageHandle((short) 30);
                }else {
                    task.setOverageHandle((short) 20);
                }
                task.setOverageSN(ylsn);
                task.setOverageWeight(new BigDecimal(yl));
            }
            task.setProductSN(task.getProductSN());
            task.setProductionLineSN(lineentity.getSn());
            task.setProductionTaskSN(taskentity.getSn());
            task.setSaleWeight(new BigDecimal(stfl).add(new BigDecimal(sjfl)));
            task.setState((short)0);
            task.setTransType((short)0);
            task.setTransWeight(new BigDecimal(stfl));
            task.setTruckCode(truckentity.getTruckCode());
            task.setTruckPlate(truckentity.getTruckPlate());
            task.setTruckSN(truckentity.getSn());
            var transtask = transTaskService.queryListByTaskCar(truckentity.getTruckCode());
            if(transtask.size()>0){
                return fail("该车辆已派，请重新选择车辆");
            }
            result = transTaskService.saveTransTask(task.toMap(),"");
            if (result > 0) {
                return success("保存成功");
            } else if(result==-10){
                return fail("此合同达到控制值！");
            }else{
                return fail("数据未保存");
            }
        } catch (Exception e) {
            return error(e);
        }
    }
}
